package com.jokesoft.supermanager.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

/**
 * Test http client
 * 
 */
public class App {

//    final static String url = "http://localhost:8080/httpservice/insertPerson";
    final static String url = "http://supermanager-joukon.rhcloud.com:80/insertPerson";

    public static void main(String[] args) {

        String request = "{\"firstName\":\"Puikko\",\"lastName\":\"Hauturi\",\"role\":\"Player\",\"addresses\":[\"Hevostilantie 22 C 14,02920,Espoo,Finland\",\"Heppakuja,70910,Vuorela,Finland\"]}";

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);

        post.setHeader("Accept", "application/json");
        post.setHeader("Content-Type", "application/json");

        try {
            post.setEntity(new StringEntity(request));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            HttpResponse response = client.execute(post);
            System.out.println("ReturnCode = " + response.getStatusLine().getStatusCode());
            System.out.println("Return = " + response.getStatusLine().toString());
            System.out.println("Return message = " + response.getEntity().getContent().toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
